package struts.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.Student;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import struts.forms.StudentForm;
import biz.studentMager.StudentMagerBiz;
import biz.studentMager.StudentMagerBziImpl;

public class StuMagerAction extends MappingDispatchAction {
	private StudentMagerBiz stuMagBiz = new StudentMagerBziImpl();

	public ActionForward getAllStudents(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List<Student> stus = (List<Student>) stuMagBiz.selAllStudent();
		request.setAttribute("students", stus);

		return mapping.findForward("getStudents");
	}

	public ActionForward deleteStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		Long stuId = Long.parseLong(request.getParameter("stuId"));
		stuMagBiz.delStudent(stuId);

		return mapping.findForward("toGetStudents");
	}

	public ActionForward addReadyStudent(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StudentForm stuForm = (StudentForm) form;
		Student student = new Student();
		stuForm.setStudent(student);

		return mapping.findForward("toAddStudent");
	}

	public ActionForward addStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentForm stuForm = (StudentForm) form;
		Student student = stuForm.getStudent();
		stuMagBiz.addStudent(student);
		return mapping.findForward("toGetStudents");
	}

	public ActionForward modifyReadyStudent(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Long stuId = Long.parseLong(request.getParameter("stuId"));
		StudentForm stuForm = (StudentForm) form;
		Student student = stuMagBiz.selStudentById(stuId);
		stuForm.setStudent(student);

		return mapping.findForward("toModifyStudent");
	}

	public ActionForward modifyStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentForm stuForm = (StudentForm) form;
		Student student = stuForm.getStudent();
		stuMagBiz.updateStudent(student);

		return mapping.findForward("toGetStudents");
	}
}
